package io.bdmc.common.utils;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class EmailUtils {

    private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);

    public static void sendHtmlMail(String host, String username, String pwd, String subject, String content,
            String receiver) {
        try {
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

            // 设定mail server
            senderImpl.setHost(host);
            senderImpl.setPort(465);
            senderImpl.setUsername(username); // 根据自己的情况,设置发件邮箱地址
            senderImpl.setPassword(pwd); // 根据自己的情况, 设置password
            senderImpl.setDefaultEncoding("UTF-8");
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            senderImpl.setJavaMailProperties(prop);

            // 建立邮件消息,发送简单邮件和html邮件的区别
            MimeMessage mailMessage = senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

            // 设置收件人，寄件人
            messageHelper.setTo(receiver);
            messageHelper.setFrom(username);
            messageHelper.setSubject(subject);
            // true 表示启动HTML格式的邮件
            messageHelper.setText(content, true);

            // 发送邮件
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            logger.info("EmailUtils.method [sendHtmlMail]: email send error message-" + e);
        }
    }

    public static void sendHtmlMail(String host, String username, String pwd, String subject, List<String> alarmMsgList,
            String receiver, boolean isAlarm) {
        String content = "";
        if (isAlarm) {
            content = getTableContent(alarmMsgList);
        } else {
            content = getNormalContent(alarmMsgList);
        }

        for (String rec : receiver.split(",")) {
            if (StringUtils.isNotEmpty(rec)) {
                sendHtmlMail(host, username, pwd, subject, content, rec);
            }
        }
    }

    private static String getNormalContent(List<String> alarmMsgList) {
        StringBuilder content = new StringBuilder("<html><head></head><body><h2>光纤中断恢复</h2>");
        content.append("<table border=\"5\" style=\"border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
        content.append(
                "<tr style=\"background-color: #428BCA; color:#ffffff\"><th>设备名称</th><th>设备SN</th><th>光缆名称</th><th>纤芯序号</th><th>恢复时间</th></tr>");
        for (String data : alarmMsgList) {
            String[] msgArr = data.split("------");
            content.append("<tr>");
            for (String s : msgArr) {
                content.append("<td>").append(s).append("</td>");
            }
            content.append("</tr>");
        }
        content.append("</table>");
        content.append("</body></html>");
        return content.toString();
    }

    /**
     * 返回组装好的表格html
     * 
     * @param alarmMsgList 报警的数组
     * @return 组装好的表格html
     */
    private static String getTableContent(List<String> alarmMsgList) {
        StringBuilder content = new StringBuilder("<html><head></head><body><h2>光纤长度告警</h2>");
        content.append("<table border=\"5\" style=\"border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
        content.append(
                "<tr style=\"background-color: #428BCA; color:#ffffff\"><th>设备名称</th><th>设备SN</th><th>光缆名称</th><th>纤芯序号</th><th>光缆长度</th><th>中断位置</th><th>经度</th><th>纬度</th><th>告警产生时间</th></tr>");
        for (String data : alarmMsgList) {
            data = data.replaceAll("光缆长度变化前", "").replaceAll("变化后", "").replaceAll("经纬度位置:", "").replaceAll(",",
                    "------");
            String[] msgArr = data.split("------");
            content.append("<tr>");
            for (String s : msgArr) {
                content.append("<td>").append(s).append("</td>");
            }
            content.append("</tr>");
        }
        content.append("</table>");
        content.append("</body></html>");
        return content.toString();
    }

}
